var  BASE = BASE||{}; //全局公用方法
$(function () {
    BASE.COMMON.urlLeftNav();
    BASE.COMMON.recommendDarenList(1,10);
    $(document).scroll(function () {
        BASE.COMMON.scrollEvent(BASE.BLOG.getData);
    });
});

BASE.BLOG = (function () {
    var PAGE = 2;
    /*弹出层*/
    function showPopup(obj) {
        BASE.POPUP.SELECTEDCOVERS = [];
        BASE.POPUP.initShowCovers();
        $('#title').val('');
        $('#price').val('');
        BASE.UPLOADANSWER.ue.txt.html('');
       
        $('#order_price').val('');
        var userId = BASE.COMMON.getUserId();
        if(!userId){
            location.href = '/login.html';
            return;
        }
        var flag = $(obj).data('flag');
        if(flag){
            $('#answer_pop').children('.pop-answer').removeClass('is-full-screen');
            $('#answer_pop').show();
            return;
        }
        $(obj).data('flag',true);
        setTimeout(function () {
            $(obj).data('flag',false);
        },500);
        BASE.POPUP.getInterest('',function () {
            $('#answer_pop').show();
            $(obj).data('flag',true);
        },true);
    }

    /*发单*/
    function sendOrder(obj) {
        var sendData = BASE.BLOGCOMMON.getAddData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/blog/add',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            window.location.reload();
        })
    }

    /*获取推荐的数据*/
    function getData() {
        var $box = $('#middleListContent');
        $box.find('.loading').show();
        var sendData = {
            pageNum:PAGE,
            pageSize:15,
        };
        BASE.apiGet('/device/blog/list',sendData,function (res) {
            ++PAGE;
            $box.find('.loading').hide();
            BASE.COMMON.SCROLLFLAG = true;
            if((res.nextPage == 0 && res.pages > 1) || res.pages <= 1){
                BASE.COMMON.SCROLLFLAG = false;
                if(res.pages!=0 && res.total>=8){
                    $box.find('.nomore-data').show();
                }
            }
            $box.find('.content').append(template('middleListContent_tpl',{data:res.list}));
        })
    }
    return {
        showPopup:showPopup,
        sendOrder:sendOrder,
        getData:getData,
    }
})();